package com.sun.lib.net.remote;

import android.content.Context;

import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.sun.lib.LibCommonManager;
import com.sun.lib.rx.Disposables;
import com.sun.lib.util.SystemUtil;
import com.sun.lib.util.ToastUtil;
import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by sunxiquan on 2018/10/26.
 * <p>
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public abstract class StringObserver implements Observer<Response<String>> {
    private Disposables disposables;
    private CompositeDisposable compositeDisposable;

    public StringObserver(Disposables disposables) {
        this.disposables = disposables;
    }

    public StringObserver(CompositeDisposable compositeDisposable) {
        this.compositeDisposable = compositeDisposable;
    }

    public StringObserver() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (disposables != null)
            disposables.add(d);
        if (compositeDisposable != null)
            compositeDisposable.add(d);
    }

    @Override
    public void onError(Throwable e) {
        onFailed(e);
    }

    @Override
    public void onComplete() {
        onFinished();
    }

    @Override
    public void onNext(Response<String> stringResponse) {
        String json = stringResponse.body();
        Logger.d(json);
        //Logger.json(json);
        if (stringResponse.isFromCache())
            onCacheSuccess(json);
        else
            onSuccess(json);

    }

    /**
     * 获取缓存数据
     *
     * @param s
     */
    protected void onCacheSuccess(String s) {

    }

    protected abstract void onSuccess(String s);

    protected void onFailed(Throwable e) {
        Context context = LibCommonManager.getContext();
        if (!SystemUtil.isNetWorkConnected(context)) {
            ToastUtil.show(context, "网络异常");
        } else {
            if (e == null) return;
            ToastUtil.show(context, "数据异常");
        }
    }

    protected void onFinished() {
    }
}
